package heven.holt.library.extensions

import android.graphics.Rect
import android.view.Window
import android.widget.FrameLayout
import heven.holt.library.listener.OnSoftInputChangedListener
import timber.log.Timber
import kotlin.math.abs

fun Window.registerSoftInputChangedListener(listener: OnSoftInputChangedListener) {
    val contentView = findViewById<FrameLayout>(android.R.id.content)
    contentView.registerSoftInputChangedListener(listener)
}

fun Window.unregisterSoftInputChangedListener() {
    val contentView = findViewById<FrameLayout>(android.R.id.content) ?: return
    contentView.unregisterSoftInputChangedListener()
}

fun Window.getDecorViewInvisibleHeight(): Int {
    val outRect = Rect()
    decorView.getWindowVisibleDisplayFrame(outRect)
    Timber.d("Window：getDecorViewInvisibleHeight：${decorView.bottom - outRect.bottom}")
    val delta = abs(decorView.bottom - outRect.bottom)
    val resource = decorView.context.resources
    if (delta <= resource.getStatusBarHeight() + resource.getNavBarHeight()) {
        return 0
    }
    return delta
}